import heapq
class Solution(object):
    def thirdMax(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums = set(nums)
        out = []
        count = 0
        for item in nums:
            if count < 3:
                heapq.heappush(out, item)
                count += 1
            else:
                if item > out[0]:
                    heapq.heapreplace(out, item)
        if count < 3:
            return out[-1]
        else:
            return out[0]



data = Solution()
nums = [2, 2, 3, 1]
print(data.thirdMax(nums))